var express = require("express");
const myroute = express.Router();
const mySql = require("../model/index");

myroute.get("/listGet", function (req, res) {
  const sql = `SELECT * FROM message_type a LEFT JOIN message b on a.m_id = b.theme ORDER BY b.id`;
  let resArr = [];
  let resArr2 = [];
  mySql.query(sql, (err, data) => {
    if (err) {
      res.send({ code: 0, err: err });
    } else {
      function render(data) {
        let res = [];
        for (let i = 0; i < data.length; i++) {
          if (res.length) {
            if (data[i].m_type == "审批" && data[i].theme == 1) {
              res[0].children.push({
                id: data[i].id,
                content: data[i].content,
                time: data[i].time,
                type: data[i].type,
              });
            }
          } else {
            if (data[i].m_type == "审批" && data[i].theme == 1) {
              res[0] = {
                m_id: data[i].m_id,
                m_type: data[i].m_type,
                img: data[i].img,
                title: data[i].title,
                children: [
                  {
                    id: data[i].id,
                    content: data[i].content,
                    time: data[i].time,
                    type: data[i].type,
                  },
                ],
              };
            }
          }
        }
        resArr[0] = res[0];
      }
      render(data);

      function render2(data) {
        let res = [];
        for (let i = 0; i < data.length; i++) {
          if (res.length) {
            if (data[i].theme == 2 && data[i].m_type == "会议") {
              res[0].children.push({
                id: data[i].id,
                content: data[i].content,
                time: data[i].time,
                type: data[i].type,
              });
            }
          } else {
            if (data[i].theme == 2 && data[i].m_type == "会议") {
              res[0] = {
                m_id: data[i].m_id,
                m_type: data[i].m_type,
                img: data[i].img,
                title: data[i].title,
                children: [
                  {
                    id: data[i].id,
                    content: data[i].content,
                    time: data[i].time,
                    type: data[i].type,
                  },
                ],
              };
            }
          }
        }
        resArr2[0] = res[0];
      }
      render2(data);
      res.send({
        code: 200,
        data: [{ resArr: resArr[0] }, { resArr2: resArr2[0] }],
      });
    }
  });
});

myroute.get("/listGet", function (req, res) {
  const sql = `SELECT * FROM message_type a LEFT JOIN message b on a.m_id = b.theme ORDER BY b.id`;
  let resArr = [];
  let resArr2 = [];
  mySql.query(sql, (err, data) => {
    if (err) {
      res.send({ code: 0, err: err });
    } else {
      function render(data) {
        let res = [];
        for (let i = 0; i < data.length; i++) {
          if (res.length) {
            if (data[i].m_type == "审批" && data[i].theme == 1) {
              res[0].children.push({
                id: data[i].id,
                content: data[i].content,
                time: data[i].time,
                type: data[i].type,
              });
            }
          } else {
            if (data[i].m_type == "审批" && data[i].theme == 1) {
              res[0] = {
                m_id: data[i].m_id,
                m_type: data[i].m_type,
                img: data[i].img,
                title: data[i].title,
                children: [
                  {
                    id: data[i].id,
                    content: data[i].content,
                    time: data[i].time,
                    type: data[i].type,
                  },
                ],
              };
            }
          }
        }
        resArr[0] = res[0];
      }
      render(data);

      function render2(data) {
        let res = [];
        for (let i = 0; i < data.length; i++) {
          if (res.length) {
            if (data[i].theme == 2 && data[i].m_type == "会议") {
              res[0].children.push({
                id: data[i].id,
                content: data[i].content,
                time: data[i].time,
                type: data[i].type,
              });
            }
          } else {
            if (data[i].theme == 2 && data[i].m_type == "会议") {
              res[0] = {
                m_id: data[i].m_id,
                m_type: data[i].m_type,
                img: data[i].img,
                title: data[i].title,
                children: [
                  {
                    id: data[i].id,
                    content: data[i].content,
                    time: data[i].time,
                    type: data[i].type,
                  },
                ],
              };
            }
          }
        }
        resArr2[0] = res[0];
      }
      render2(data);
      res.send({
        code: 200,
        data: [{ resArr: resArr[0] }, { resArr2: resArr2[0] }],
      });
    }
  });
});
// 岗位渲染
myroute.get("/getpost", function (req, res) {
  const mysql = `
    SELECT * FROM postadmin a left JOIN department b  ON a.post_department=b.id
`;
  mySql.query(mysql, function (err, data) {
    if (err) {
      res.send({ code: 1, err: err });
    } else {
      res.send({ code: 200, data: data });
    }
  });
});

// 岗位删除
myroute.post("/postdel", function (req, res) {
  const mysqlDel = `DELETE FROM postadmin WHERE post_id =${req.body.id};`;
  const mysqlDel2 = ` SELECT a.post_name,b.name FROM postadmin a left JOIN admin b  ON a.post_id=b.jobs JOIN department c ON a.post_department = c.id`;
  mySql.query(mysqlDel, function (err, data) {
    if (err) {
      res.send({ code: 1, err: err });
    } else {
      mySql.query(mysqlDel2, function (err2, data2) {
        if (err2) {
          res.send({ code2: 1, err: err2 });
        } else {
          res.send({ code: 200, data: [data, data2] });
        }
      });
    }
  });
});

// 岗位编辑
myroute.post("/getBianJi", function (req, res) {
  let num;
  if (req.body.buMenId == "人事部") {
    num = 1;
  } else if (req.body.buMenId == "行政部") {
    num = 2;
  } else if (req.body.buMenId == "财务部") {
    num = 3;
  } else if (req.body.buMenId == "销售部") {
    num = 4;
  } else {
    num = 5;
  }

  let mysqlBJ = `UPDATE postadmin SET post_name='${req.body.name}',post_department=${num},post_state='${req.body.state}',post_time='${req.body.time}',
    superior="${req.body.suiji}" 
    WHERE post_id=${req.body.id}`;
  if (req.body.name)
    mySql.query(mysqlBJ, function (err, data) {
      if (err) {
        res.send({ code: 1, err: err });
      } else {
        res.send({ code: 200, data: data });
      }
    });
});

// 岗位查询
myroute.get("/getChaXun", function (req, res) {
  let mysqlC = `SELECT * from postadmin a   JOIN department b on a.post_department=b.id
    WHERE post_name LIKE '%${req.query.name}%' AND department_name LIKE '%${req.query.department_name}%' AND post_state LIKE '%${req.query.state}%'`;
  mySql.query(mysqlC, function (err, data) {
    if (err) {
      res.send({ code: 1, err: err });
    } else {
      res.send({ code: 200, data: data });
    }
  });
});

// 岗位新增
myroute.post("/getAdd", function (req, res) {
  let mysqlAdd = `INSERT INTO postadmin VALUES(NULL,'${req.body.name}',${req.body.department},'${req.body.state}','系统管理员','${req.body.time}',${req.body.suiji})`;
  mySql.query(mysqlAdd, function (err, data) {
    if (err) {
      res.send({ code: 1, err: err });
    } else {
      res.send({ code: 200, data: data });
    }
  });
});

//

// 人员渲染
myroute.get("/empPost", function (req, res) {
  const mysql = `select * from admin a LEFT JOIN postadmin b on a.jobs=b.post_id
    LEFT JOIN department c on  b.post_department = c.id`;
  mySql.query(mysql, function (err, data) {
    if (err) {
      res.send({ code: 1, err: err });
    } else {
      res.send({ code: 0, data: data });
    }
  });
});

// 人员删除
myroute.post("/empDel", function (req, res) {
  const mysqlDel = `DELETE FROM admin WHERE work_id =${req.body.id};`;
  const mysqlDel2 = ` select * from admin a LEFT JOIN postadmin b on a.jobs=b.post_id
    LEFT JOIN department c on  b.post_department = c.id`;
  mySql.query(mysqlDel, function (err, data) {
    if (err) {
      res.send({ code: 1, err: err });
    } else {
      mySql.query(mysqlDel2, function (err2, data2) {
        if (err2) {
          res.send({ code2: 1, err: err2 });
        } else {
          res.send({ code: 200, data: [data, data2] });
        }
      });
    }
  });
});

// 人员编辑
myroute.post("/empBianJi", function (req, res) {
  let mysqlBJ = `  
    UPDATE admin a  SET a.name='${req.body.name}' ,a.jobs=${req.body.postId} , a.is_admin="${req.body.region}"
    WHERE a.work_id=${req.body.id} `;
  if (req.body.name)
    mySql.query(mysqlBJ, function (err, data) {
      if (err) {
        res.send({ code: 1, err: err });
      } else {
        res.send({ code: 200, data: data });
      }
    });
});

// 编辑选择部门 返回岗位
myroute.get("/empPostArray", function (req, res) {
  const mysql = `SELECT a.post_name,b.department_name,a.post_time,a.post_id from postadmin a LEFT JOIN department b on a.post_department=b.id
                   WHERE b.department_name='${req.query.department}' `;
  mySql.query(mysql, function (err, data) {
    if (err) {
      res.send({ code: 1, err: err });
    } else {
      res.send({ code: 200, data: data });
    }
  });
});

// 人员查询
myroute.get("/empChaXun", function (req, res) {
  let mysqlCX = `select  * from admin a LEFT JOIN postadmin b on a.jobs=b.post_id
    LEFT JOIN department c on  b.post_department = c.id
    WHERE a.name LIKE '%${req.query.name}%'  AND b.post_name LIKE '%${req.query.post}%' AND c.department_name LIKE '%${req.query.department}%'
    ORDER BY work_id`;
  console.log(req);
  mySql.query(mysqlCX, function (err, data) {
    if (err) {
      res.send({ code: 0, err: err });
    } else {
      res.send({ code: 200, data: data });
    }
  });
});

// 人员新增
myroute.post("/addEmp", function (req, res) {
  console.log(req.body);
  let mysqlAdd2 = `
    INSERT INTO admin VALUES(${req.body.work_id},${req.body.account},${req.body.password},
    '${req.body.type}','','',${req.body.jobs},'${req.body.sex}',${req.body.phone},'',${req.body.idcard},null,'${req.body.name}',
    '${req.body.is_admin}','${req.body.time}')`;
  mySql.query(mysqlAdd2, function (err, data) {
    if (err) {
      res.send({ code: 0, err: err });
    } else {
      res.send({ code: 200, data: data });
    }
  });
});

// 新增选择部门 返回岗位
myroute.get("/empPostArray2", function (req, res) {
  const mysql = `SELECT post_id,a.post_name,b.department_name,a.post_time from postadmin a LEFT JOIN department b on a.post_department=b.id
    WHERE b.department_name='${req.query.department}'`;
  mySql.query(mysql, function (err, data) {
    if (err) {
      res.send({ code: 1, err: err });
    } else {
      res.send({ code: 200, data: data });
    }
  });
});

//

// 组织建构一进来的显示
myroute.get("/getOrg", function (req, res) {
  let orgSql = [
    `select  a.name,b.post_id,b.post_name,c.department_name,a.is_admin from admin a LEFT JOIN postadmin b on a.jobs=b.post_id
    LEFT JOIN department c on  b.post_department = c.id
    WHERE post_name LIKE '%主管%'`,
    `
        select * from postadmin a right JOIN department b on a.post_department=b.id  RIGHT JOIN corporate_name c on  b.or_id =c.dep_id
                WHERE post_name LIKE '%主管%'
        `,
  ];
  mySql.query(orgSql[0], function (err, data) {
    if (err) {
      res.send({ code: 0, err: err });
    } else {
      mySql.query(orgSql[1], function (err2, data2) {
        if (err2) {
          res.send({ code: 0, err2: err2 });
        } else {
          res.send({ code: 200, data: [data, data2] });
        }
      });
    }
  });
});

// 点击切换
myroute.get("/qieHuanOrg", function (req, res) {
  let orgSql = [
    `select  a.name,b.post_id,b.post_name,c.department_name,a.is_admin from admin a LEFT JOIN postadmin b on a.jobs=b.post_id
    LEFT JOIN department c on  b.post_department = c.id
    WHERE department_name='${req.query.department}'`,
    `select  a.name,b.post_id,b.post_name,c.department_name,a.is_admin from admin a LEFT JOIN postadmin b on a.jobs=b.post_id
    LEFT JOIN department c on  b.post_department = c.id
		WHERE department_name='${req.query.department}'
   AND post_name LIKE '%主管%'
`,
  ];
  mySql.query(orgSql[0], function (err, data) {
    if (err) {
      res.send({ code: 0, err: err });
    } else {
      mySql.query(orgSql[1], function (err2, data2) {
        if (err) {
          res.send({ code: 0, err2: err2 });
        } else {
          res.send({ code: 200, data: data, data2: data2 });
        }
      });
    }
  });
});

// 点击编辑获取 Ajax 下拉框
myroute.get("/OrgPostArray", function (req, res) {
  const mysql = `SELECT a.post_name,b.department_name,a.post_time,a.post_id from postadmin a LEFT JOIN department b on a.post_department=b.id
                   WHERE b.department_name='${req.query.department}' `;
  mySql.query(mysql, function (err, data) {
    if (err) {
      res.send({ code: 1, err: err });
    } else {
      res.send({ code: 200, data: data });
    }
  });
});

//

myroute.get("/ceshi", function (req, res) {
  const mysql = `select * from department a JOIN corporate_name b on a.or_id=b.dep_id`;
  mySql.query(mysql, function (err, data) {
    if (err) {
      res.send({ code: 1, err: err });
    } else {
      function merge(arr) {
        let tempArr = [];
        let endData = [];
        for (let i = 0; i < arr.length; i++) {
          if (tempArr.indexOf(arr[i].or_name) === -1) {
            endData.push({
              label: arr[i].or_name,
              id: arr[i].id,
              children: [{ label: arr[i].department_name, id: arr[i].id }],
            });
            tempArr.push(arr[i].or_name);
          } else {
            for (let j = 0; j < endData.length; j++) {
              if (endData[j].label == arr[i].or_name) {
                endData[j].children.push({
                  label: arr[i].department_name,
                  id: arr[i].id,
                });
                break;
              }
            }
          }
        }
        return endData; // 最终输出
      }
      merge(data);
      res.send({ code: 200, data: merge(data) });
    }
  });
});

module.exports = myroute;
